import React, { useState, useEffect } from 'react';
import IconFont from '@/components/IconFont';
import { Tooltip, Form, Radio } from 'antd';
import cn from 'classnames';
import { size } from 'lodash';
import styles from './index.less';
import PermissionModule from './components/PermissionModule';

// 权限管理-数据权限
const DataPermission = ({ form }) => {
    const { getFieldDecorator, getFieldValue } = form;
    const authorityListData = getFieldValue('authorityList');

    const [moduleData, setModuleData] = useState();
    const [firstLoad, setFirstLoad] = useState(true); // 由于第3步的数据源是来自于第2步，第2步又会默认赋值！！只能写个开关控制是否校验，同时表单上加force

    useEffect(() => {
        const tempArray = authorityListData
            ?.filter(item => {
                const { WEB, APP, PC } = item.selectedScope;
                return item.resourceType === 1 && (WEB || APP || PC); // 取出模块类型且至少有一个端勾选的
            })
            .map(item => {
                const { resourceCode, ...rest } = item;
                return { ...rest, resourceModuleCode: resourceCode };
            });
        setModuleData(tempArray);
    }, [authorityListData]);

    return (
        <div className={styles.dataPermissionWrap}>
            <div className={styles.label}>
                <Tooltip title="允许数据的查询和导出" placement="topLeft" arrowPointAtCenter>
                    <IconFont icon="exclamation-circle" style={{ marginRight: 5, color: 'rgba(0, 0, 0, 0.45)' }} />
                </Tooltip>
                数据权限：
            </div>
            <div className={styles.content}>
                <Form.Item>
                    {getFieldDecorator('dataScopeSetType')(
                        <Radio.Group>
                            <Radio value={0}>全局统一</Radio>
                            <Radio value={1}>模块控制</Radio>
                        </Radio.Group>
                    )}
                </Form.Item>
                <Form.Item
                    className={cn(
                        styles.authorityDataListItem,
                        getFieldValue('dataScopeSetType') === 1 ? styles.typeModuleItem : ''
                    )}
                >
                    {getFieldDecorator('authorityDataList', {
                        rules: [
                            {
                                validator: (rule, value = [], callback) => {
                                    // 第二步没有模块不需要校验
                                    if (size(moduleData) === 0) {
                                        callback();
                                    } else if (firstLoad === true) {
                                        setFirstLoad(false);
                                        callback();
                                    } else {
                                        const tempArr = value?.filter(
                                            item =>
                                                item.administrativeScope === undefined &&
                                                item.organizationalScope === undefined
                                        );
                                        if (size(tempArr) === size(value)) {
                                            callback('请完成数据权限设置');
                                        } else {
                                            // 校验指定公司或者指定部门不能为空
                                            // 全局统一只要判断第一项！
                                            let arr = [];
                                            if (getFieldValue('dataScopeSetType') === 0) {
                                                arr.push(value[0]);
                                            } else {
                                                arr = value;
                                            }
                                            let errType;
                                            for (let i = 0; i < arr.length; i += 1) {
                                                if (arr[i].organizationalScope === undefined) {
                                                    errType = 1;
                                                    break;
                                                }
                                                if (arr[i].administrativeScope === undefined) {
                                                    errType = 2;
                                                    break;
                                                }
                                                if (
                                                    arr[i].administrativeScope === 4 &&
                                                    arr[i].companyCodeList.length === 0
                                                ) {
                                                    errType = 3;
                                                    break;
                                                }
                                                if (
                                                    arr[i].organizationalScope === 5 &&
                                                    arr[i].orgCodeList.length === 0
                                                ) {
                                                    errType = 4;
                                                    break;
                                                }
                                            }
                                            // if (errType === 1) {
                                            //     callback('请选择组织机构');
                                            // } else if (errType === 2) {
                                            //     callback('请选择行政机构');
                                            // } else if (errType === 3) {
                                            //     callback('请选择指定公司');
                                            // } else if (errType === 4) {
                                            //     callback('请选择指定部门');
                                            // }
                                            if (errType) {
                                                callback('请完成数据权限设置');
                                            } else {
                                                callback();
                                            }
                                        }
                                    }
                                }
                            }
                        ]
                    })(<PermissionModule selectType={getFieldValue('dataScopeSetType')} data={moduleData} />)}
                </Form.Item>
            </div>
        </div>
    );
};
export default DataPermission;
